<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true">
				<el-form-item>
					<el-input v-model="keyword" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="warning" v-on:click="keywordQuery">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表v-loading="listLoading"-->
		<el-table :data="tableDatas" v-loading="listLoading" @selection-change="handleSelectionChange"
			@cell-dblclick="openDrawer" highlight-current-row style="width: 100%;">

			<!--多选框-->
			<el-table-column type="selection" width="80">
			</el-table-column>
			<el-table-column type="index" width="300" label="编号">
			</el-table-column>
			<el-table-column prop="dictName" label="字典名称" width="300">
			</el-table-column>
			<el-table-column prop="sn" label="编号" width="350">
			</el-table-column>
			<el-table-column label="操作">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" :disabled="this.sels.length === 0" @click="handlePatchDel">批量删除</el-button>
			<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
				:current-page="currentPage" :page-sizes="[5, 10, 15]" :page-size="pageSize"
				layout="total, sizes, prev, pager, next, jumper" :total="totals" style="float:right;">
			</el-pagination>
		</el-col>

		<!--添加或修改界面-->
		<el-dialog :title="title" :visible.sync="saveFormVisible" :close-on-click-modal="false">
			<el-form :model="saveForm" label-width="80px" ref="saveForm">
				<el-form-item label="字典名称">
					<el-input v-model="saveForm.dictName" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="字典编号">
					<el-input v-model="saveForm.sn" auto-complete="off"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="saveFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="saveSubmit">提交</el-button>
			</div>
		</el-dialog>

		<!-- ==========================================抽屉========================================== -->
		<el-drawer title="字典明细" :visible.sync="drawer" :direction="direction" :before-close="handleClose" size="50%">
			<!--工具条-->
			<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
				<el-form :inline="true">
					<el-form-item>
						<el-input v-model="itemKeyword" placeholder="关键字"></el-input>
					</el-form-item>
					<el-form-item>
						<el-button type="warning" v-on:click="itemKeywordQuery">查询</el-button>
					</el-form-item>
					<el-form-item>
						<el-button type="primary" @click="itemHandleAdd">新增</el-button>
					</el-form-item>
				</el-form>
			</el-col>

			<!--列表v-loading="itemListLoading"-->
			<el-table :data="itemTableDatas" v-loading="itemListLoading" @selection-change="itemHandleSelectionChange"
			  highlight-current-row style="width: 100%;">
				<!--多选框-->
				<el-table-column type="selection" width="80">
				</el-table-column>
				<el-table-column type="index" width="200" label="编号">
				</el-table-column>
				<el-table-column prop="itemName" label="字典明细名称" width="300">
				</el-table-column>
				<el-table-column label="操作">
					<template scope="scope">
						<el-button size="small" @click="itemHandleEdit(scope.row)">编辑</el-button>
						<el-button type="danger" size="small" @click="itemHandleDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<!--工具条-->
			<el-col :span="24" class="toolbar">
				<el-button type="danger" :disabled="this.itemSels.length === 0" @click="itemHandlePatchDel">批量删除</el-button>
				<el-pagination @size-change="itemHandleSizeChange" @current-change="itemHandleCurrentChange" :current-page="itemCurrentPage"
				 :page-sizes="[5, 10, 15]" :page-size="itemPageSize"
					layout="total, sizes, prev, pager, next, jumper" :total="itemTotals" style="float:right;">
				</el-pagination>
			</el-col>

			<!--添加或修改界面-->
			<el-dialog :title="itemTitle" :visible.sync="itemSaveFormVisible" :close-on-click-modal="false" width="500px" style="right: 33% !important;" append-to-body>

				<el-form :model="itemSaveForm" label-width="80px" ref="itemSaveForm">
					<el-form-item label="字典名称">
						<el-input v-model="itemSaveForm.itemName" auto-complete="off"></el-input>
					</el-form-item>
				</el-form>
				<div slot="footer" class="dialog-footer">
					<el-button @click.native="itemSaveFormVisible = false">取消</el-button>
					<el-button type="primary" @click.native="itemSaveSubmit">提交</el-button>
				</div>
			</el-dialog>

		</el-drawer>
	</section>
</template>

<script>
export default {
	data() {
		return {
			row:null,// 确保初始化，避免 undefined
			currentDictRow:null,// 新增：记录当前操作的字典行
			//1.分页查询相关数据
			totals: 0,//总记录条数
			currentPage: 1,//当前页,要传递到后台的
			pageSize: 5, //每页显示多少条
			tableDatas: [], //当前页数据
			listLoading: false,

			//2.高级查询相关数据
			keyword: '',
			//3.删除相关数据

			//4.批量删除相关数据
			sels: [], //存储多选记录
			title: '',
			//5.添加和修改相关数据
			saveFormVisible: false,
			saveForm: {
				id: null,
				dictName: '',
				sn: ''
			},
			/* ============================================抽屉============================================ */
			drawer: false,
			direction: 'rtl',
			//1.分页查询相关数据
			itemTotals: 0,//总记录条数
			itemCurrentPage: 1,//当前页,要传递到后台的
			itemPageSize: 5, //每页显示多少条
			itemTableDatas: [], //当前页数据
			itemListLoading: false,

			//2.高级查询相关数据
			itemKeyword: '',
			//3.删除相关数据
			dictId:null,
			//4.批量删除相关数据
			itemSels: [], //存储多选记录
			itemTitle: '',
			//5.添加和修改相关数据
			itemSaveFormVisible: false,
			itemSaveForm: {
				id: null,
				dictId: '',
				itemName: '',
			},
		}
	},
	methods: {
		//1.分页查询相关方法
		//1.1.获取列表数据
		getTableData() {
			let condition = {
				keyword: this.keyword,
				pageSize: this.pageSize,
				currentPage: this.currentPage
			}
			this.$http.post('/dict', condition).then((res) => {
				this.tableDatas = res.data.data.list;
				this.totals = res.data.data.total;
			}).catch((res) => {
				this.$message("服务器繁忙");
			});
		},
		//1.2.选择第几页时触发
		handleCurrentChange(curentPage) {
			this.currentPage = curentPage;
			this.getTableData()
		},
		//1.3.选择每页显示记录条数时触发
		handleSizeChange(pageSize) {
			this.pageSize = pageSize;
			this.getTableData()
		},

		//2.高级查询相关方法
		keywordQuery() {
			this.currentPage = 1;
			this.getTableData()
		},

		//3.删除相关方法
		handleDel(row) {
			this.$confirm('此操作将永久删除该字典管理, 是否继续?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				this.$http.delete('/dict/' + row.id).then((res) => {
					if (res.data.code == 200) {
						this.$message.success("删除成功");
						this.getTableData()
					} else {
						this.$message.error("删除失败");
					}
				}).catch((res) => {
					this.$message.error("服务器繁忙");
				});
			}).catch(() => {
				this.$message({
					type: 'info',
					message: '已取消删除'
				});
			});

		},


		//4.批量删除相关方法
		//4.1.选项改变时触发
		handleSelectionChange(val) {
			console.log(val);
			this.sels = val.map(row => row.id);
			console.log(this.sels);

		},
		//4.2.批量删除
		handlePatchDel() {
			this.$confirm('此操作将批量删除字典管理, 是否继续?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				this.$http.patch('/dict/', this.sels).then((res) => {
					if (res.data.code == 200) {
						this.$message.success("删除成功");
						this.getTableData()
					} else {
						this.$message.error("删除失败");
					}
				}).catch((res) => {
					this.$message.error("服务器繁忙");
				});
			}).catch(() => {
				this.$message({
					type: 'info',
					message: '已取消删除'
				});
			});
		},

		//5.添加和修改相关方法
		//5.1.点击添加弹出对话框
		handleAdd() {
			this.title = '添加数据字典';
			this.saveFormVisible = true;
			this.saveForm = {};

		},
		//5.2.点击修改弹出对话框
		handleEdit(row) {
			this.title = '编辑';
			this.saveFormVisible = true;
			// this.form = row;//数据回显,不能这样写
			this.saveForm = Object.assign({}, row);

		},
		//5.3.提交表单数据操作数据库
		saveSubmit() {
			this.$refs['saveForm'].validate((valid) => {
				if (valid) {
					this.saveFormVisible = false;
					this.$http.put('/dict', this.saveForm).then((res) => {
						if (res.data.code == 200) {

							//新增成功,跳转到新增位置
							if (this.saveForm.id == null) {
								this.totals++;
								this.currentPage = Math.ceil(this.totals / this.pageSize);
							}
							this.getTableData();
						} else {
							this.$message.error(res.data.msg);
						}
					}).catch(() => {
						this.$message.error("网络异常，请稍后重试！【400/404/...】");
					})

				} else {
					this.$message.error("表单校验未通过,请填写必填项");
					return false;
				}
			});
		},
		openDrawer(row) {
			this.drawer = true;
			this.currentDictRow = row; // 新增：记录当前字典行数据
			this.getItemTableData(row);
			this.dictId = row.id; // 加载抽屉列表数据
		},
		handleClose(done) {
			this.$confirm('确认关闭？')
				.then(_ => {
					done();
				})
				.catch(_ => { });
		},
		//=====================================抽屉=======================================
		getItemTableData(row) {
			let condition = {
				itemKeyword: this.itemKeyword,
				itemPageSize: this.itemPageSize,
				itemCurrentPage: this.itemCurrentPage,
				dictId: row ? row.id : this.dictId // 兼容可能的空值
			}
			
			this.$http.post('/dictItem', condition).then((res) => { 
				this.itemTableDatas = res.data.data.list;
				this.itemTotals = res.data.data.total;
			}).catch((res) => {
				this.$message("服务器繁忙");
			});
		},
		//1.2.选择第几页时触发
		itemHandleCurrentChange(curentPage) {
			this.itemCurrentPage = curentPage;
			this.getItemTableData(this.currentDictRow); 
		},
		//1.3.选择每页显示记录条数时触发
		itemHandleSizeChange(pageSize) {
			this.itemPageSize = pageSize;
			this.getItemTableData();
		},

		//2.高级查询相关方法
		itemKeywordQuery() {
			this.itemCurrentPage = 1;
			this.getItemTableData(this.currentDictRow);
		},

		//3.删除相关方法
		itemHandleDel(row) {
			this.$confirm('此操作将永久删除该字典明细 是否继续?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				this.$http.delete('/dictItem/' + row.id).then((res) => {
					if (res.data.code == 200) {
						this.$message.success("删除成功");
						this.getItemTableData(this.currentDictRow); // 刷新抽屉列表
					} else {
						this.$message.error("删除失败");
					}
				}).catch((res) => {
					this.$message.error("服务器繁忙");
				});
			}).catch(() => {
				this.$message({
					type: 'info',
					message: '已取消删除'
				});
			});

		},


		//4.批量删除相关方法
		//4.1.选项改变时触发
		itemHandleSelectionChange(val) {
			console.log(val);
			this.itemSels = val.map(row => row.id);
			console.log(this.itemSels);

		},
		//4.2.批量删除
		itemHandlePatchDel() {
			this.$confirm('此操作将批量删除字典明细, 是否继续?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				this.$http.patch('/dictItem/', this.itemSels).then((res) => {
					if (res.data.code == 200) {
						this.$message.success("删除成功");
						this.getTableData()
					} else {
						this.$message.error("删除失败");
					}
				}).catch((res) => {
					this.$message.error("服务器繁忙");
				});
			}).catch(() => {
				this.$message({
					type: 'info',
					message: '已取消删除'
				});
			});
		},

		//5.添加和修改相关方法
		//5.1.点击添加弹出对话框
		itemHandleAdd() {
			this.itemTitle = '添加数据字典明细';
			this.itemSaveFormVisible = true;
			this.itemSaveForm = {};

		},
		//5.2.点击修改弹出对话框
		itemHandleEdit(row) {  // 方法名与模板绑定一致
		this.itemTitle = '编辑';
		this.itemSaveFormVisible = true; // 打开编辑对话框
		this.itemSaveForm = Object.assign({}, row); // 回显数据
		},
		//5.3.提交表单数据操作数据库
		itemSaveSubmit() {
			this.$refs['itemSaveForm'].validate((valid) => { 
				if (valid) {
					this.itemSaveFormVisible = false; 
					this.itemSaveForm.dictId = this.dictId;
					this.$http.put('/dictItem', this.itemSaveForm).then((res) => {
						if (res.data.code == 200) {

							//新增成功,跳转到新增位置
							if (this.itemSaveForm.id == null) {
								this.itemTotals++;
								this.itemCurrentPage = Math.ceil(this.itemTotals / this.itemPageSize);
							}
							this.getItemTableData(this.currentDictRow);

						} else {
							this.$message.error(res.data.msg);
						}
					}).catch(() => {
						this.$message.error("网络异常，请稍后重试！【400/404/...】");
					})

				} else {
					this.$message.error("表单校验未通过,请填写必填项");
					return false;
				}
			});
		},
		//=====================================抽屉=======================================
	},
	mounted() {
		this.getTableData();
	}
}

</script>

<style scoped></style>
